﻿package com.gengine.ui.timer
{
    import com.gengine.core.frame.*;
    import flash.display.*;
    import flash.text.*;

    public class GameClock extends Sprite
    {
        private var _year:int;
        private var _month:int;
        private var _date:int;
        private var _hours:int;
        private var _minutes:int;
        private var _seconds:int;
        private var _time:Number;
        private var _nowDate:Date;
        private var _secTimer:SecTimer;
        private var _label:TextField;
        public var enterFrameHandler:Function;

        public function GameClock()
        {
            this.mouseChildren = false;
            this.mouseEnabled = false;
            this._label = new TextField();
            this._label.mouseEnabled = false;
            this._label.autoSize = TextFieldAutoSize.CENTER;
            this._label.defaultTextFormat = new TextFormat(null, 12);
            this.addChild(this._label);
            this._secTimer = new SecTimer();
            this._secTimer.addListener(TimerType.ENTERFRAME, this.onEnterFrameHandler);
            return;
        }// end function

        public function get nowDate() : Date
        {
            if (!this._nowDate)
            {
                this._nowDate = new Date();
            }
            return this._nowDate;
        }// end function

        private function onEnterFrameHandler(param1:SecTimer) : void
        {
            (this._nowDate.seconds + 1);
            if (this.parent)
            {
                this._label.text = this.getDateStr();
            }
            if (this.enterFrameHandler is Function)
            {
                this.enterFrameHandler(this._nowDate);
            }
            return;
        }// end function

        public function set time(param1:Object) : void
        {
            if (param1 is Date)
            {
                this._nowDate = param1 as Date;
            }
            else if (param1 is Number)
            {
                this._time = param1 as Number;
                this._nowDate = new Date(this._time);
            }
            if (this._secTimer.running == false)
            {
                this._secTimer.start();
            }
            return;
        }// end function

        private function getDateStr() : String
        {
            var _loc_1:String = "";
            _loc_1 = _loc_1 + (this._nowDate.fullYear + "-");
            if (this._nowDate.month < 10)
            {
                _loc_1 = _loc_1 + "0";
            }
            _loc_1 = _loc_1 + ((this._nowDate.month + 1) + "-");
            if (this._nowDate.date < 10)
            {
                _loc_1 = _loc_1 + "0";
            }
            _loc_1 = _loc_1 + (this._nowDate.date + "  ");
            if (this._nowDate.hours < 10)
            {
                _loc_1 = _loc_1 + "0";
            }
            _loc_1 = _loc_1 + (this._nowDate.hours + ":");
            if (this._nowDate.minutes < 10)
            {
                _loc_1 = _loc_1 + "0";
            }
            _loc_1 = _loc_1 + (this._nowDate.minutes + ":");
            if (this._nowDate.seconds < 10)
            {
                _loc_1 = _loc_1 + "0";
            }
            _loc_1 = _loc_1 + this._nowDate.seconds;
            return _loc_1;
        }// end function

    }
}
